package com.oxchinx.order.controller;

import com.oxchinx.order.client.ProductClient;
import com.oxchinx.order.dto.CartDTO;
import com.oxchinx.order.model.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * Created by dell on 2018/5/23.
 * 服务间通信
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class ClientController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductClient productClient;

    /**
     * 第一种方式
     * 缺点，url 固定写死
     */
    @GetMapping("/getProductMsg1")
    public String getProductMsg1(){

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://localhost:8080/msg1",String.class);
        log.info("response = {}",response);
        return response;
    }


    /**
     * 第二种方式
     * 利用loadBalancerClient 通过应用名获取url，然后使用RestTemplate调用接口
     * PRODUCT 為应用的主机名
     */
    @GetMapping("/getProductMsg2")
    public String getProductMsg2(){

        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        String url = String.format("http://%s:%s",host,port) + "/msg2";
        String response = restTemplate.getForObject(url,String.class);
        log.info("response = {}",response);
        return response;
    }

    /**
     * 第三种方式
     *使用注解的方式,利用@LoadBalanced，可在restTemplate里面使用应用的名字
     * PRODUCT 為应用的主机名
     */
    @GetMapping("/getProductMsg3")
    public String getProductMsg3(){
        String response = restTemplate.getForObject("http://PRODUCT/msg3",String.class);
        log.info("response = {}",response);
        return response;
    }

    /**
     * 使用feign调用接口
     */
    @GetMapping("/getProductMsg4")
    public String getProductMsg4(){
        String response = productClient.productMsg();
        log.info("response = {}",response);
        return response;
    }

    @GetMapping("/getProductList")
    public String getProductList(){
       List<ProductInfo> productInfoList =  productClient.listForOrder(Arrays.asList("157875227953464068","164103465734242707"));
        log.info("response = {}",productInfoList);
        return "ok";
    }

    @GetMapping("/productDecreaseStock")
    public String productDecreaseStock(){
        productClient.decreaseStock(Arrays.asList(
                new CartDTO("157875227953464068",3),
                new CartDTO("164103465734242707",5)));
        return "ok";
    }

}
